package com.heer.gymnasium.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.Interface.OnHeightChangeListener;
import com.heer.gymnasium.R;
import com.heer.gymnasium.adapter.SiteDetailsTicketAdapter;
import com.heer.gymnasium.util.ViewUtils;
import com.heer.model.SitePlaceInfoBean;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

import static com.heer.gymnasium.constant.Extras.EXTRA_ID;

/**
 * Created by heer on 17/4/11.
 */

public class SiteDetailsTicketFragment extends BaseFragment {
    @BindView(R.id.list)
    ListView list;
    private OnHeightChangeListener listener;
    private SiteDetailsTicketAdapter mAdapter;
    private String id;

    public static SiteDetailsTicketFragment newInstance(String id) {
        SiteDetailsTicketFragment fragment = new SiteDetailsTicketFragment();
        Bundle args = new Bundle();
        args.putString(EXTRA_ID, id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments() != null ? getArguments().getString(EXTRA_ID) : "";
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            listener = (OnHeightChangeListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + "must implement OnHeightChangeListener");
        }
    }

    @Override
    protected int setConvertView() {
        return R.layout.fragment_site_details_ticket;
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void initView() {
        super.initView();
        list.setAdapter(mAdapter = new SiteDetailsTicketAdapter(mContext));
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        listener.onHeightChange(id, ViewUtils.getListViewHeightBasedOnChildren(list));
    }

    @Override
    protected void loadData(boolean loadmore) {
        mAppHelper.apiService().getSitePlaceInfoList(id).enqueue(new BaseCallback<ApiResponse<List<SitePlaceInfoBean>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<SitePlaceInfoBean>>> call, Response<ApiResponse<List<SitePlaceInfoBean>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful() && response.body().isSccessful()) {
                    List<SitePlaceInfoBean> list = response.body().getData();
                    if (null != list && list.size() > 0)
                        mAdapter.setItems(list);
                }
                if(response.body().isTokenExpire()){
                    toLogin();
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<SitePlaceInfoBean>>> call, Throwable t) {
                super.onFailure(call, t);
            }
        });
    }
}
